<?php

namespace app\adminapi\controller\v1\exam;

use app\adminapi\controller\AuthController;
use app\exceptions\AuthException;
use app\services\exam\ExamSendService;
use think\facade\App;

class ExamSend extends AuthController
{

    public function __construct(App $app, ExamSendService $service)
    {
        parent::__construct($app);
        $this->services = $service;
    }

    public function index()
    {
        $where = $this->request->getMore([
            ['website_id', $this->website],
            ['page', 1],
            ['pageSize', 20],
            ['title', ''],
            ['pattern', ''],
        ]);
        return app()->make('json')->successful($this->services->index($where));
    }

    public function createOrUpdate()
    {
        $where = $this->request->getMore([
            ['id', ''],
            ['title', ''],
            ['status', ''],
            ['class_id', ''],
            ['paper_id', ''],
            ['title', ''],
            ['total_score', ''],
            ['pass_score', ''],
            ['pass_score', ''],
            ['duration', ''],
            ['pattern', 1],
            ['exam_num', 1],
            ['exam_model', 1],
            ['video_monitor', 1],
            ['face', 0],
            ['chop_screen', ''],
            ['screen_num', ''],
            ['exam_late', ''],
            ['late_time', ''],
            ['exam_shor', ''],
            ['shor_time', ''],
            ['exam_tian', 0],
            ['start_time', ''],
            ['end_time', ''],
            ['website_id', $this->website],
        ]);

        return app()->make('json')->successful($this->services->createOrUpdate($where));
    }

    public function delete()
    {
        $where = $this->request->getMore([
            ['id', ''],
        ]);
        if (!$where['id']) throw new AuthException('缺少必要参数');
        return app()->make('json')->successful($this->services->delete($where));
    }

    public function getPushUrl()
    {

        $where = $this->request->getMore([
            ['idcard', ''],//用户身份证号
            ['eid', ''],//考试批次
        ]);
        $where = ['domain' => 'livepush.jsonbug.com',
            'streamName' => $where['idcard'] . $where['eid'],//唯一
            'key' => 'f3e152893fb689dfe2fe5910b451eb10',
            'time' => date('Y-m-d H:i:s', time() + 3600 * 3)];//3小时 过期
        return app()->make('json')->successful($this->services->getPushUrl($where['domain'], $where['streamName'], $where['key'], $where['time']));
    }


}